import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;

public class Test1 {
    public static void main1(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() ->{
            System.out.println(1);
        });
        Thread t2 = new Thread(() ->{
            System.out.println(2);
        });
        Thread t3 = new Thread(() ->{
            System.out.println(3);
        });
        Thread t4 = new Thread(() ->{
            System.out.println(4);
        });
        //......

        t1.start();
        t2.start();
        t3.start();
        t4.start();

        t1.join();
        t2.join();
        t3.join();
        t4.join();

        System.out.println("OK");
    }



    public static void main(String[] args) throws SQLException {
        Scanner scanner = new Scanner(System.in);

        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/test_8_28?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("root");

        int id = scanner.nextInt();
        String name = scanner.next();

        Connection connection = dataSource.getConnection();
        String sql = "insert into student values(?, ?)";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setInt(1, id);
        preparedStatement.setString(2, name);
        int n = preparedStatement.executeUpdate();
        System.out.println(n);

        sql = "update student set name = '小丑' where id = 7";
        preparedStatement = connection.prepareStatement(sql);
        n = preparedStatement.executeUpdate();
        System.out.println(n);

        sql = "delete from student where id = 8";
        preparedStatement = connection.prepareStatement(sql);
        n = preparedStatement.executeUpdate();
        System.out.println(n);

        sql = "select * from student";
        preparedStatement = connection.prepareStatement(sql);
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            int id2 = resultSet.getInt("id");
            String name2 = resultSet.getString("name");
            System.out.println(id2 + " " + name2);
        }

        resultSet.close();
        preparedStatement.close();
        connection.close();

    }
}
